{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 快乐python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 知识点总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基础知识\n",
    "\n",
    "> 1、Python 访问子字符串 可以使用方括号来截取字符串(里边的数字叫做索引，注意：**索引从0开始取，而不是从1开始**\n",
    "\n",
    "> 2、python模块(Module)，是一个 Python 文件，以 .py 结尾，包含了 Python 对象定义和Python语句。Python 标准库中包含了大量的模块（称为标准模块），还有大量的第三方模块，开发者自己也可以开发自定义模块。\n",
    "常用基础模块有：**os模块、sys模块、datetime,date,time模块、random模块**\n",
    " \n",
    "\n",
    "### 变量名称\n",
    "\n",
    "    Python 变量命名规则：\n",
    "* 变量名必须以字母或下划线字符开头\n",
    "* 变量名称不能以数字开头\n",
    "* 变量名只能包含字母数字字符和下划线（A-z、0-9 和 _）\n",
    "* 变量名称区分大小写（age、Age 和 AGE 是三个不同的变量）\n",
    "* 请记住，变量名称区分大小写\n",
    "\n",
    "### 模块\n",
    "1.理解:Python 模块(Module)，是一个 Python 文件，以 .py 结尾，包含了 Python 对象定义和Python语句。Python **标准库**中包含了大量的模块（称为标准模块），还有**大量的第三方模块**，开发者自己也可以开发**自定义模块**。\n",
    "\n",
    "2.功能:模块能定义函数，类和变量，模块里也能包含可执行的代码\n",
    " \n",
    "3.常用基础模块: \n",
    "* os模块:①os.remove()：删除一个文件,②os.rename：重命名文件,③os.path模块\n",
    "\n",
    "* sys模块:①sys.path 返回模块的搜索路径②sys.platform 返回操作系统平台名称③sys.stdout 标准输出\n",
    "\n",
    "* datetime,date,time模块:①datetime.date.today() 本地日期对象②datetime.date.fromtimestamp() 返回一个日期对象，参数是时间戳,返回 年-月-日③datetime.date.weekday(obj) 返回一个日期对象的星期数,周一是0\n",
    "\n",
    "* random模块:①random.random() 产生0-1的随机浮点数②random.uniform(a, b) 产生指定范围内的随机浮点数\n",
    "\n",
    "* string模块:①str.capitalize() 把字符串的第一个字符大写②str.lower() 将大写转为小写 str.upper() 转换字符串的小写为大写\n",
    "\n",
    "\n",
    "### 标准库\n",
    "1.理解:函数+模块=标准库\n",
    "\n",
    "2.\"Python的标准库有\":'datetime','zlib','random','math','sys','glob','os'\n",
    "\n",
    "3.第三方库: \n",
    "\n",
    "* Scrapy：①爬虫工具常用的库。\n",
    "[参考链接](https://blog.csdn.net/alice_tl/article/details/81433214)\n",
    "\n",
    "* Pillow:①是PIL（Python图形库）的一个分支。适用于在图形领域工作的人\n",
    "* OpenCV:①图片识别常用的库，通常在练习人脸识别时会用到\n",
    "* pytesseract:①图片文字识别，即OCR识别 。[参考链接](https://blog.csdn.net/alice_tl/article/details/89299405)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自评"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python_base={\n",
    "       \"IDE\":{  \n",
    "               \"IDLE\":[\"操作，100（100）\",\"意义、100（100）\",\n",
    "               \"JUPYTE\":\"操作，100（99）\",\"意义、100（99）\",\n",
    "               \"pycharm\":\"操作，100（99）\",\"意义、100（99）\",\n",
    "               \"基础知识\":\"操作，100（95）\",\"意义、100（100）\",\n",
    "                      ]\n",
    "                       \n",
    "               }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自评"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "自评列表数据={\n",
    "   \"总结\":{    \n",
    "           \"字符串\":[\"操作，100（99）\",\"意义、100（100）\"]\n",
    "           \"列表\":[\"操作，100（95）\",\"意义、100（100）\"]\n",
    "           \"字典\":[\"操作，100（95）\",\"意义、100（100）\"]\n",
    "           \"元组\":[\"操作，100（95）\",\"意义、100（100）\"]\n",
    "           \"集合\":[\"操作，100（95）\",\"意义、100（100）\"]\n",
    "                  \n",
    "   }\n",
    "           }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串\n",
    "\n",
    "1.理解:字符串是 Python 中最常用的数据类型。我们可以使用引号( ' 或 \" )来创建字符串。\n",
    "\n",
    "2.功能:访问、拼接、运算、内建函数\n",
    " \n",
    "3.常用操作: \n",
    "* 字符串拼接:对字符串进行截取并使用“+”号与其他字符串进行连接\n",
    "\n",
    "* 转义字符:在需要在字符中使用特殊字符时，python 用反斜杠 \\ 转义字符。 常用：\\n 换行、\\v 纵向制表']\n",
    "\n",
    "\n",
    "转义字符|描述\n",
    "---|---:\n",
    "\\\\(在行尾时)|续行符(一行代码过长不利于阅读)\n",
    "\\\\\\ |反斜杠符号(写代码时需要显示\\时用到)\n",
    "\\\\'|单引号(写代码时需要显示'时用到)\n",
    "\\\\\"|双引号(写代码时需要显示\"时用到)\n",
    "\n",
    "\n",
    "\n",
    "* 4.字符串内建函数: \n",
    "\n",
    "①.split()通过指定分隔符对字符串转换成列表\n",
    "②.count():用于统计字符串里某个字符出现的次数。\n",
    "③.isdigit():如果 string 只包含数字则返回 True 否则返回 False                               "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.baidu.com\n"
     ]
    }
   ],
   "source": [
    "# 字符串拼接\n",
    "url = 'www.baidu.com'\n",
    " \n",
    "print( \"https://\"+url)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计对象出现次数\n",
    "str = \"this is string example....wow!!!\";\n",
    "c=str.count(\"i\")\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# 检查数据类型  \n",
    "str = \"123456\";  # Only digit in this string\n",
    "print(str.isdigit())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 内置数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **一、列表**\n",
    "\n",
    "\n",
    "1.理解:列表是有序的可变对象集合。 Python中的列表用 中括号“ [ ]” 包围，里面的数据之间**用 逗号“,”** 进行分隔。         \n",
    "\n",
    "2.注意:序列中的每个元素都分配一个数字 - 它的位置，或索引，**第一个索引是0**，第二个索引是1，依此类推。               \n",
    "                \n",
    "3.功能:可以进行的操作包括*索引，切片，加，乘，检查成员。*\n",
    "[复习链接](https://www.runoob.com/python/python-lists.html)\n",
    "\n",
    "\n",
    "4.python列表函数&方法:                    \n",
    "* ①list.append(obj)：在列表末尾添加新的对象\n",
    "* ②list.count(obj)：统计某个元素在列表中出现的次数\n",
    "* ③list.extend(seq)：在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）\n",
    "* ④list.pop([index=-1])：移除列表中的一个元素（默认最后一个元素），并且返回该元素的值\n",
    "* ⑤list.remove(obj)：移除列表中某个值的第一个匹配项\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Karry', 'Jack', 'Wendy', 'Anna']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Karry\",\"Jack\",\"Wendy\",\"Anna\"]\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Anna\n"
     ]
    }
   ],
   "source": [
    "print(names[-1]) #切片 取出最后一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(names[-3:-1]) #切片 从后往前取出一些值，不包括索引位置-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表的增、删、改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['physics', 'chemistry', 1997, 2000]\n"
     ]
    }
   ],
   "source": [
    "list1 = ['physics', 'chemistry', 1997, 2000]\n",
    " \n",
    "print(list1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After deleting value at index 2 : \n",
      "['physics', 'chemistry']\n"
     ]
    }
   ],
   "source": [
    "del list1[2]\n",
    "print(\"After deleting value at index 2 : \")\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **二、字典**\n",
    "\n",
    "\n",
    "1.理解:字典是另一种可变容器模型，且可存储任意类型对象。包围，里面的数据之间**用 逗号“,” 进行分隔。**         \n",
    "\n",
    "2.注意:字典是无序的，所以不存在位置关系；字典的每个键值 key=>value 对用冒号 : 分割，每个键值对之间用逗号 , 分割，整个字典包括在**花括号 {} 中**，d = {key1 : value1, key2 : value2 }。               \n",
    "                \n",
    "3.知识点:熟悉字典的*增删查改、内置函数以及方法*\n",
    "[复习链接](https://docs.python.org/zh-cn/3/tutorial/index.html)\n",
    "\n",
    "\n",
    "4.python字典函数&方法:                    \n",
    "* ①dict.clear()：删除字典内所有元素\n",
    "* ②dict.fromkeys(seq[, val])：创建一个新字典，以序列 seq 中元素做字典的键，val 为字典所有键对应的初始值\n",
    "* ③dict.items()：以列表返回可遍历的(键, 值) 元组数组\n",
    "* ④dict.keys()：以列表返回一个字典所有的键\n",
    "* ⑤ict.values() ：以列表返回一个字典所有的值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1': 78, 'stu2': 88, 'stu3': 98}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的增删查改\n",
    "stu  = {'stu1':78 ,'stu2': 88, 'stu3': 98 }\n",
    "stu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1': 78, 'stu2': 88, 'stu3': 98, 'stu5': 108}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu['stu5']=108 # 直接添加元素 字典名['键'] = 值\n",
    "stu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Siri', 'Age': 17, 'Class': 'First'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict2 = {'Name': 'Siri', 'Age': 17, 'Class': 'First'}\n",
    "dict2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "del dict2['Name']  # 删除键是'Name'的条目 del字典名['键名']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Age': 17, 'Class': 'First'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Zara', 'Age': 8, 'Class': 'First'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3 = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}\n",
    "dict3['Age'] = 8 # 改值 字典{'键名'] = 新值\n",
    "dict3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'女'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3['姓名']= '女' # 改键\n",
    "dict3['姓名']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **三、元组**\n",
    "\n",
    "\n",
    "1.理解:Python的元组与列表类似，不同之处在于**元组的元素不能修改**。创建只需要在括号中添加元素，并使用逗号隔开即可。\n",
    "\n",
    "2.注意:元组中只包含**一个元素时**，需要在元素后面**添加逗号**          \n",
    "                \n",
    "3.知识点:熟悉元组的增删查、内置函数以及方法\n",
    "[复习链接](https://www.runoob.com/python/python-tuples.html)\n",
    "\n",
    "\n",
    "4.python元组函数&方法:                    \n",
    "* ①cmp(tuple1, tuple2)：比较两个元组元素\n",
    "* ②len(tuple)：计算元组元素个数\n",
    "* ③max(tuple)：返回元组中元素最大值\n",
    "* ④min(tuple)：返回元组中元素最小值\n",
    "* ⑤tuple(seq)：将列表转换为元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tup1[0]:  physics\n",
      "tup2[1:5]:  (2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "# 访问元组\n",
    "tup1 = ('physics', 'chemistry', 1997, 2000)\n",
    "tup2 = (1, 2, 3, 4, 5, 6, 7 )\n",
    " \n",
    "print(\"tup1[0]: \", tup1[0])\n",
    "print(\"tup2[1:5]: \", tup2[1:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12, 34.56, 'abc', 'xyz')\n"
     ]
    }
   ],
   "source": [
    "# 修改元组\n",
    "tup1 = (12, 34.56)\n",
    "tup2 = ('abc', 'xyz')\n",
    "tup3 = tup1 + tup2\n",
    "print(tup3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **四、集合**\n",
    "\n",
    "\n",
    "1.理解:集合（set）是一个**无序的不重复元素序列**。可以使用大括号 { } 或者 set() 函数创建集合\",               \n",
    "               \n",
    "2.注意:创建一个空集合必须用** set() 而不是 { }**，因为 { } 是用来创建一个空字典。\",                \n",
    "                     \n",
    "3.知识点:熟悉集合的增删查改、计算、内置函数以及方法\n",
    "[复习链接](https://www.runoob.com/python3/python3-set.html)\n",
    "\n",
    "\n",
    "4.python集合函数&方法:                    \n",
    "* ①add()：为集合添加元素\n",
    "* ②clear()：移除集合中的所有元素\n",
    "* ③isdisjoint()：\t判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False\n",
    "* ④pop()：随机移除元素\n",
    "* ⑤remove()：移除指定元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Google', 'Facebook', 'Taobao', 'Runoob'}\n"
     ]
    }
   ],
   "source": [
    "# 添加元素\n",
    "thisset = set((\"Google\", \"Runoob\", \"Taobao\"))\n",
    "thisset.add(\"Facebook\")\n",
    "print(thisset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Google', 'Runoob'}\n"
     ]
    }
   ],
   "source": [
    "# 移除元素\n",
    "thisset = set((\"Google\", \"Runoob\", \"Taobao\"))\n",
    "thisset.remove(\"Taobao\")\n",
    "print(thisset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算集合元素\n",
    "thisset = set((\"Google\", \"Runoob\", \"Taobao\"))\n",
    "len(thisset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 扩展：list循环遍历"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 方法1：使用for循环简单结构遍历\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 新建一个城市列表\n",
    "cities = [\"Guangzhou\",\"Beijing\",\"Shanghai\",\"Nanjing\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Guangzhou\n",
      "Beijing\n",
      "Shanghai\n",
      "Nanjing\n"
     ]
    }
   ],
   "source": [
    "#第一种方法，直接使用简单的for循环结构[  for i in list:  print(list)  ]\n",
    "for city in cities:\n",
    "\n",
    "    print(city)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 Guangzhou\n",
      "2 Beijing\n",
      "3 Shanghai\n",
      "4 Nanjing\n"
     ]
    }
   ],
   "source": [
    "#第二种方法，利用range() 函数跟len() 函数。这种方法可以输出序列号，也可以不输出\n",
    "cities = [\"Guangzhou\",\"Beijing\",\"Shanghai\",\"Nanjing\"]\n",
    "\n",
    "for i in range(len(cities)):\n",
    "\n",
    "    print(i+1,cities[i])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 Guangzhou\n",
      "2 Beijing\n",
      "3 Shanghai\n",
      "4 Nanjing\n"
     ]
    }
   ],
   "source": [
    "# 第三种方法，利用enumerate() 函数。\n",
    "cities = [\"Guangzhou\",\"Beijing\",\"Shanghai\",\"Nanjing\"]\n",
    "\n",
    "for i,city in enumerate(cities):\n",
    "\n",
    "    print(i+1,city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Guangzhou\n",
      "Beijing\n",
      "Shanghai\n",
      "Nanjing\n"
     ]
    }
   ],
   "source": [
    "# 第四种方法，利用iter() 函数\n",
    "cities = [\"Guangzhou\",\"Beijing\",\"Shanghai\",\"Nanjing\"]\n",
    "\n",
    "for city in iter(cities):\n",
    "\n",
    "    print(city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## python条件语句\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自评"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "自评条件语句={\n",
    "   \"总结\":{    \n",
    "           \"if条件语句\":[\"操作，100（99）\",\"意义、100（100）\"]\n",
    "           \"for循环\":[\"操作，100（95）\",\"意义、100（100）\"]\n",
    "           \"while循环\":[\"操作，100（95）\",\"意义、100（100）\"]\n",
    "            \n",
    "   }\n",
    "           }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 一、内容理解\n",
    "\n",
    "* if else作为Python的条件语句，其中\"判断条件\"成立时，则执行后面的语句，而执行内容可以多行，以缩进来区分表示同一范围。\n",
    "* else 为可选语句，当需要在条件不成立时执行内容则可以执行相关语句。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 二、案例演示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的年龄：19\n",
      "软件正在使用中...\n"
     ]
    }
   ],
   "source": [
    "# 案例一\n",
    "age = int( input(\"请输入你的年龄：\") )\n",
    "if age < 18 :\n",
    "    print(\"你还未成年，建议在家人陪同下使用该软件！\")\n",
    "#该语句不属于if的代码块\n",
    "print(\"软件正在使用中...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "luren\n"
     ]
    }
   ],
   "source": [
    "# 例2：if 基本用法\n",
    " \n",
    "flag = False\n",
    "name = 'luren'\n",
    "if name == 'python':         # 判断变量是否为 python \n",
    "    flag = True              # 条件成立时设置标志为真\n",
    "    print('welcome boss')     # 并输出欢迎信息\n",
    "else:\n",
    "    print(name)               # 条件不成立时输出变量名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "roadman\n"
     ]
    }
   ],
   "source": [
    "# 例3：elif用法\n",
    " \n",
    "num = 5     \n",
    "if num == 3:            # 判断num的值\n",
    "    print ('boss')        \n",
    "elif num == 2:\n",
    "    print('user')\n",
    "elif num == 1:\n",
    "    print('worker')\n",
    "elif num < 0:           # 值小于零时输出\n",
    "    print( 'error')\n",
    "else:\n",
    "    print( 'roadman')     # 条件均不成立时输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 三、for循环\n",
    "\n",
    "* for循环可以遍历任何序列的项目，如一个列表或者一个字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 实例一 循环迭代数字变量\n",
    "for i in [1,2,3]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "# 实例二 循环迭代字符串变量\n",
    "for daima in \"Python\":\n",
    "    print(daima)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 四、range函数\n",
    "\n",
    "* range() 函数可创建一个整数列表，一般用在 for 循环中\n",
    "* range(10) 意思为从0开始到第10个数字，即[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "*  range(1, 11) 意思为从1开始到第11个数字，即[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "好冷啊\n",
      "好冷啊\n",
      "好冷啊\n",
      "好冷啊\n",
      "好冷啊\n"
     ]
    }
   ],
   "source": [
    "# 实例三 迭代指定的次数\n",
    "for shuijiao in range(5): \n",
    "    print('好冷啊')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "6\n",
      "10\n",
      "15\n",
      "21\n",
      "28\n",
      "36\n",
      "45\n",
      "55\n",
      "66\n",
      "78\n",
      "91\n",
      "105\n",
      "120\n",
      "136\n",
      "153\n",
      "171\n",
      "190\n",
      "210\n"
     ]
    }
   ],
   "source": [
    "# 实操 用for循环求解整数1+2+3+…+20的值\n",
    "sum=0\n",
    "for i in range(1,21):\n",
    "    sum = sum + i \n",
    "    print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 五、While循环\n",
    "\n",
    " * while语句用于循环执行程序，即在某条件下，循环执行某段程序，以处理需要重复处理的相同任务。简而言之，就是死循环。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我好想喝奶茶 0\n",
      "我好想喝奶茶 1\n",
      "我好想喝奶茶 2\n",
      "我好想喝奶茶 3\n",
      "我好想喝奶茶 4\n",
      "我好想喝奶茶 5\n",
      "我好想喝奶茶 6\n",
      "我好想喝奶茶 7\n",
      "我好想喝奶茶 8\n",
      "我好想喝奶茶 9\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while True:\n",
    "    print(\"我好想喝奶茶\",count)\n",
    "    count +=1  # count +=1即为count+1的意思\n",
    "    if count == 10:  #加入结束条件\n",
    "        break               #跳出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## python 函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 知识理解\n",
    "1、函数是组织好的，可重复使用的，用来实现单一，或相关联功能的代码段。\n",
    "\n",
    "2、函数能提高应用的模块性，和代码的重复利用率。你已经知道Python提供了许多内建函数，比如print()。但你也可以自己创建函数，这被叫做用户自定义函数。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### python的参数分类\n",
    "1、python参数可以分为两类：1.定义时的参数——形参（形式参数）、2.调用时的参数——实参（实际参数，传参）\n",
    "\n",
    "2、**形参和实参的区别**\n",
    "形参就是形式上的参数，可以理解为数学的X，没有实际的值，通过别人赋值后才有意义。相当于变量。\n",
    "实参就是实际意义上的参数，是一个实际存在的参数，可以是字符串或是数字等。\n",
    "\n",
    "***注意：传入实参时，一定要加上引号，否则会报错。***\n",
    "\n",
    "\n",
    "3、**实参的规则**：实参就是在函数调用的时候，通过函数后面的括号传递给函数，让函数处理的值\n",
    "\n",
    "4、**形参的规则**：形参（即函数在定义时**用来占位置的参数**）可以分为1、不带缺省值的参数、2、带缺省值的参数、3、可变位置参数、4、可变关键字参数、5、key-word only参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "#代码示例如下，其中name为形参：\n",
    "\n",
    "def greeting(name):\n",
    "\n",
    "    print(\"Hello,\"+name.title()+\"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,Lily!\n"
     ]
    }
   ],
   "source": [
    "greeting(\"Lily\")  # 如果函数定义时有形参，则调用这个函数时需提供实质的参数，调用时的参数称为“实参”。代码示例如下，其中“Lily”为实参："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 再看一个有参数的函数定义及调用实例。如下函数中，形参为x，实参为9。\n",
    "\n",
    "def squares_fun(x):\n",
    "\n",
    "    print(x ** 2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81\n"
     ]
    }
   ],
   "source": [
    "squares_fun(9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义一个函数方法\n",
    "\n",
    "* 函数代码块以 def 关键词开头，后接函数标识符名称和圆括号()。\n",
    "* 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。\n",
    "* 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。\n",
    "* 函数内容以冒号起始，并且缩进。\n",
    "* return [表达式] 结束函数，选择性地返回一个值给调用方。不带表达式的return相当于返回 None。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 例子\n",
    "def printme( str ):\n",
    "#   \"\"打印传入的字符串到标准显示设备上\"\"\n",
    "   print( str)\n",
    "   return"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数调用\n",
    "\n",
    "定义一个函数只给了函数一个名称，指定了函数里包含的参数，和代码块结构。\n",
    "\n",
    "这个函数的基本结构完成以后，你可以通过另一个函数调用执行，也可以直接从Python提示符执行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义函数\n",
    "def printme( str ):\n",
    "   \"打印任何传入的字符串\"\n",
    "   print(str)\n",
    "   return \n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我要调用用户自定义函数!\n",
      "再次调用同一函数\n"
     ]
    }
   ],
   "source": [
    "# 调用函数\n",
    "printme(\"我要调用用户自定义函数!\")\n",
    "printme(\"再次调用同一函数\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自评"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "自评函数={\n",
    "   \"总结\":{    \n",
    "           \"函数\":[\"操作，100（80）\",\"意义、100（95）\"]          \n",
    "   }\n",
    "           }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
